using System.Collections.Generic;
using System.IO;

namespace Engine.FileFormats3D.C11Undercover.QADFile
{
    internal class PointerTable
    {
        #region Private Fields

        private List<uint> completePosition;
        private List<uint> offsets;
        private long startpositionOfBlock;

        #endregion Private Fields

        #region Public Properties

        public List<uint> OffsetsCompletePosition
        {
            get
            {
                return this.completePosition;
            }
        }

        public int StartpositionOfBlock
        {
            get
            {
                return (int)this.startpositionOfBlock;
            }
        }

        #endregion Public Properties

        #region Public Methods

        public static PointerTable FromStream(BinaryReader br, int numberOfBlocks)
        {
            PointerTable pointerTable = new PointerTable();

            pointerTable.offsets = new List<uint>();
            pointerTable.completePosition = new List<uint>();
            pointerTable.startpositionOfBlock = br.BaseStream.Position;

            for (int i = 0; i < numberOfBlocks * 16; i++)
            {
                uint value = br.ReadUInt32();
                pointerTable.completePosition.Add(value + (uint)pointerTable.startpositionOfBlock);
                pointerTable.offsets.Add(value);
            }
            return pointerTable;
        }

        #endregion Public Methods

        #region Internal Methods

        internal void Save(BinaryWriter bw, CollisionBlock[] list, int numberOfBlocks)
        {
            uint offset = (uint)(numberOfBlocks * 4); // This is the position where The Collissionblocks starts after the offset table.
            this.offsets = new List<uint>();
            for (int i = 0; i < list.Length; i++)
            {
                this.offsets.Add((uint)(offset));
                offset += (uint)list[i].SizeInBytes;
            }

            for (int i = 0; i < this.offsets.Count; i++)
            {
                bw.Write(this.offsets[i]);
            }
        }

        #endregion Internal Methods
    }
}